class Game {
  // 文本刷新定时器
  #_textFlashTimer
  // 计时定时器
  #_timing
  // 总字数
  #_total
  // 正确的字数
  #_correct
  // 当前的秒数
  #_second
  // 输入字数
  #_inputNum
  // 出现字的速度
  #speed
  #_score


  get score() {
    return this.#_score;
  }

  set score(value) {
    this.#_score = value;
  }

  get textFlashTimer() {
    return this.#_textFlashTimer;
  }

  set textFlashTimer(value) {
    this.#_textFlashTimer = value;
  }

  get timing() {
    return this.#_timing;
  }

  set timing(value) {
    this.#_timing = value;
  }

  get total() {
    return this.#_total;
  }

  set total(value) {
    this.#_total = value;
  }

  get correct() {
    return this.#_correct;
  }

  set correct(value) {
    this.#_correct = value;
  }

  get second() {
    return this.#_second;
  }

  set second(value) {
    this.#_second = value;
  }

  get inputNum() {
    return this.#_inputNum;
  }

  set inputNum(value) {
    this.#_inputNum = value;
  }


  get speed() {
    return this.#speed;
  }

  set speed(value) {
    this.#speed = value;
  }

  // 单词库
  static WORKBANK = [
    'A',
    'smartphone',
    'with',
    'a',
    'displayed',
    'Micron',
    'logo',
    'is',
    'placed',
    'on',
    'a',
    'computer',
    'motherboard',
    'in',
    'this',
    'illustration',
    'taken',
    'March',
    'By',
    'singling',
    'out',
    'American',
    'chip',
    'maker',
    'Micron,',
    'Beijing',
    'is',
    'sending',
    'a',
    'message',
    'that',
    'companies',
    'aiding',
    'and',
    'abetting',
    'Washington',
    'should',
    'think',
    'twice,',
    'according',
    'to',
    'an',
    'editorial',
    'published',
    'by',
    'the',
    'South',
    'China',
    'Morning',
    'Post',
    'on',
    'Monday',
    'China',
    'has',
    'imposed',
    'a',
    'ban',
    'on',
    'products',
    'from',
    'Micron,',
    'a',
    'step',
    'that',
    'is',
    'remarkably',
    'measured',
    'when',
    'compared',
    'to',
    'the',
    'long',
    'list',
    'of',
    'export',
    'controls',
    'and',
    'tech',
    'sanctions',
    'implemented',
    'by',
    'the',
    'United',
    'States,',
    'the',
    'article',
    'wrote',
    'Ironically,',
    'Washington',
    'is',
    'now',
    'accusing',
    'China',
    'of',
    'not',
    'adhering',
    'to',
    'open',
    'commitments,',
    'in',
    'light',
    'of',
    'what',
    'many',
    'observers',
    'have',
    'termed',
    'its',
    'chip',
    'war',
    'against',
    'China,',
    'according',
    'to',
    'the',
    'editorial.',
    'So',
    'far,',
    'this',
    'US',
    'tech',
    'war',
    'has',
    'been',
    'a',
    'unilateral',
    'action,',
    'it',
    'said.',
    'The',
    'ban',
    'on',
    'Micron',
    'products',
    'demonstrates',
    'Beijing',
    'means',
    'business.',
    'The',
    'Cyberspace',
    'Administration',
    'of',
    'China',
    'stated',
    'that',
    "Micron's",
    'products',
    'have',
    'serious',
    'hidden',
    'problems',
    'of',
    'network',
    'security,',
    'which',
    'posed',
    'network',
    'security',
    'risks',
    'to',
    'critical',
    'information',
    'infrastructure',
    'supply',
    'chain',
    'and',
    'therefore',
    'security',
    'The',
    'action',
    'against',
    'the',
    'semiconductor',
    'giant',
    'may',
    'eventually',
    'lead',
    'to',
    'the',
    'company',
    'being',
    'shut',
    'out',
    'of',
    'the',
    'Chinese',
    'market,',
    'the',
    'editorial',
    'warned.',
    'Micron',
    'is',
    'almost',
    'the',
    'only',
    'company',
    'that',
    'not',
    'only',
    'initiates',
    'multiple',
    'intellectual',
    'property',
    'lawsuits',
    'but',
    'also',
    'frequently',
    'lobbies',
    'Washington',
    'against',
    'China.',
    'According',
    'to',
    'records,',
    'Micron',
    'has',
    'lobbied',
    'the',
    'US',
    'government',
    'at',
    'least',
    '170',
    'times',
    'in',
    'the',
    'past',
    'five',
    'years',
    'on',
    'issues',
    'related',
    'to',
    'competition',
    'with',
    'China.',
    'Some',
    'Chinese',
    'memory',
    'chip',
    'manufacturers',
    'listed',
    'on',
    'the',
    "US's",
    'blacklist',
    'are',
    'either',
    'direct',
    'or',
    'potential',
    'competitors',
    'of',
    'Micron.',
    'Such',
    'strategies',
    'have',
    'backfired.',
    'It',
    'has',
    'been',
    'reported',
    'that',
    'the',
    'Biden',
    'administration',
    'has',
    'pressured',
    'the',
    'South',
    'Korean',
    'government',
    'and',
    'Samsung',
    'not',
    'to',
    'attempt',
    'to',
    'fill',
    'the',
    'supply',
    'gap',
    'left',
    'by',
    'Micron.',
    'It',
    'remains',
    'unclear',
    'whether',
    'the',
    'South',
    'Korea',
    'will',
    'comply',
    'with',
    'the',
    'US',
    'request',
    'However,',
    'domestic',
    'chip',
    'suppliers',
    'in',
    'China',
    'have',
    'already',
    'taken',
    'action.',
    'Top',
    'Chinese',
    'memory',
    'chip',
    'makers',
    'have',
    'made',
    'significant',
    'progress.',
    'They',
    'are',
    'testing',
    'domestic',
    'equipment',
    'and',
    'have',
    'enough',
    'confidence',
    'to',
    'rely',
    'on',
    'domestic',
    'suppliers',
    'to',
    'replace',
    'key',
    'components',
    'By',
    'singling',
    'out',
    'Micron,',
    'China',
    'is',
    'sending',
    'an',
    'unmistakable',
    'message:',
    'companies',
    'assisting',
    'and',
    'instigating',
    'the',
    'US',
    'government',
    'should',
    'think',
    'twice,',
    'the',
    'article',
    'said.',
    'They',
    'cannot',
    'afford',
    'to',
    'lose',
    "China's",
    'huge',
    'market.',
    'And',
    'playing',
    'a',
    'double',
    'game',
    'like',
    'Micron',
    'will',
    'just',
    'backfire',
    'on',
    'them',
    'it',
    'added'
  ]

  constructor(speed = 5) {
    this.#speed = speed
    this.#_correct = 0
    this.#_total = 0
    this.#_inputNum=0
    this.#_score = 0
    this.#_second=0
  }
}

$(function () {

  const game = new Game(5)

  let randomNum = -1


  alert('设置游戏模式需要将游戏暂停')

  function choose() {
    $('#hard>li:not(:first-child)').css({ display: 'none' })

    $('#hard').mousemove(() => {
      $('#hard>li:not(:first-child)').css({ display: 'block' })
    })

    const hards = $('#hard>li')
    for (let i = 0; i < hards.length; i++) {
      $(hards[i]).click(() => {
        // 1.交换元素的位置
        $(hards[i]).insertBefore('#hard>li:nth-child(1)')
        // 2.将外层的data-hard设置为 对应的难度，并将元素设置为不隐藏
        $('#hard').data('hard', $(hards[i]).data('hard'))
        const hard = $(hards[i]).data('hard')
        if (hard === 'normal') {
          game.speed = 5
        } else if (hard === 'slower') {
          game.speed = 8
        } else if (hard === 'faster') {
          game.speed = 4
        } else if (hard === 'hard') {
          game.speed = 3
        }
      })
    }

    $('#hard').mouseleave(() => {
      $('#hard>li:not(:first-child)').css({ display: 'none', transition: '0.5s all' })
    })
  }

  function gameStartOrPuased() {
    $('#game-option').click(() => {
      let gameStart = $('#game-option').data('option')

      if (!gameStart) {
        $('#game-option').data('option', true)
        $('#game-option>span').text('暂停游戏')
        console.log(game.speed)
        game.textFlashTimer = setInterval(randomFlashText, game.speed * 1000)
        scoreCalculation()
        game.timing = setInterval(() => {
          timeCalculation()
          game.second++
        }, 1000)
      } else {
        $('#game-option').data('option', false)
        $('#game-option>span').text('开始游戏')
        clearInterval(game.textFlashTimer)
        clearInterval(game.timing)
      }
    })
  }

  // 随机出现英文单词
  function randomFlashText() {
    $('#text-input').val('')
    game.inputNum = 0
    randomNum = Math.round(Math.random() * (Game.WORKBANK.length - 1 - 1) + 1)
    game.total += Game.WORKBANK[randomNum].length
    $('#text>span').text(Game.WORKBANK[randomNum])
  }

  function scoreCalculation() {
    // 实时监听input是否输入了
    $('#text-input').bind('input propertychange', () => {
      let inputText = $('#text-input').val() + ''
      if (game.inputNum < Game.WORKBANK[randomNum].length) {
        if (inputText[game.inputNum] == Game.WORKBANK[randomNum][game.inputNum]) {
          game.correct++
        }
      }
      game.score = Math.round((game.correct / game.total) * 100)
      game.inputNum++

      // 改变得分
      $('#score>span').text(game.score)
    })
  }

  // 计时器
  function timeCalculation() {
    $('#time>span').text(game.second)
  }

  choose()
  gameStartOrPuased()
})
